package com.vn.gofriends.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.vn.gofriends.R;
import com.vn.gofriends.R.id;
import com.vn.gofriends.adapter.MenuAdapter;
import com.vn.gofriends.item.Trip;

public class ItemMyTripFragment extends Fragment {
	// Declare Variables
	Button btnAddGroup;
	MenuAdapter menuAdapter;
	GridView grMenu;
	TextView txtTitleItemTrip;
	TextView txtNameMember;

	Trip iTrip;
	ParseQuery<ParseObject> query;
	String nameMember = "";
	@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View view = null;
			view = inflater.inflate(R.layout.mytrips_layout, container, false);
			grMenu = (GridView) view.findViewById(R.id.grMenu);
			txtTitleItemTrip = (TextView) view.findViewById(R.id.txtTitleItemTrip);
			txtNameMember = (TextView) view.findViewById(R.id.txtNameMember);
			//
			//database
			String[] dataName = {"Map" , "Attactions" , "Tours&Activities","Hotel","Restaurants","Gas station"};
			int[] img = null;
			//end database
			Bundle bundle = this.getArguments();
			iTrip = (Trip) bundle.getSerializable("KeyTrip");
			txtTitleItemTrip.setText(iTrip.getTitle());
			//Check Member
			ParseObject traveler = new ParseObject("Traveler");
			ParseQuery<ParseObject> queryTraveler = ParseQuery.getQuery("Traveler");
			queryTraveler.include("idUser");
			queryTraveler.include("idTrip");
			
			queryTraveler.findInBackground(new FindCallback<ParseObject>() {
				
				@Override
				public void done(List<ParseObject> travelers, ParseException e) {
				if (e == null) {
					for (ParseObject t : travelers) {
						String idTrip;
						idTrip = t.getParseObject("idTrip")
								.getObjectId().trim();
						Log.v("Vien","ID trip = "+ idTrip + " I Trip = " +iTrip.getId() + "Username = "+ iTrip.getUserameOwner() );
						if (idTrip.equals(iTrip.getId().trim())) {
							nameMember += t.getParseObject("idUser")
									.getString("username") + " ";
						}
					}
					txtNameMember.setText(nameMember);
				}	
					else{
						Log.d("Vien", e.getMessage());
					}
				}
			});
			//Check Member End
			//load data menu
			menuAdapter = new MenuAdapter(getActivity(), dataName, img);
			grMenu.setAdapter(menuAdapter);
			//end load data
			btnAddGroup = (Button) view.findViewById(R.id.btnAddGroup);

			btnAddGroup.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					
				}
			});
			return view;
		}
}
